package com.campus.payment;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.partnerpayments.jsapi.JsapiService;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.*;

import java.util.Date;

/** JsapiService使用示例 */
public class JsapiServiceExample {

  /** 商户号 */
  public static String merchantId = "1720005941";

  /** 商户API私钥路径 */
  public static String privateKeyPath = "G:\\项目\\张永波\\服务器\\商户API证书\\apiclient_key.pem";

  /** 商户证书序列号 */
  public static String merchantSerialNumber = "237A5B8620A5EB9F83EC45F8D483E79F4EF0F309";

  /** 商户APIV3密钥 */
  public static String apiV3Key = "6ba7b8109dad11d180b400c04fd430c8";

  /**
   * 微信支付公钥ID
   */
  public static String publicKeyId = "PUB_KEY_ID_0117200059412025062700191583001400";

  /**
   * 微信支付公钥路径
   */
  public static String publicKeyPath = "G:\\项目\\张永波\\服务器\\pub_key.pem";


  public static JsapiService service;

  public static void main(String[] args) {
    // 初始化商户配置
    Config config = new RSAPublicKeyConfig.Builder()
            .merchantId(merchantId)
            .privateKeyFromPath(privateKeyPath)
            .merchantSerialNumber(merchantSerialNumber)
            .publicKeyFromPath(publicKeyPath)
            .publicKeyId(publicKeyId)
            .apiV3Key(apiV3Key)
            .build();


    // 初始化服务
    service = new JsapiService.Builder().config(config).build();
    // ... 调用接口
    try {
      PrepayResponse prepay = prepay();
      System.out.println("预支付交易会话标识="+prepay.getPrepayId());
//      closeOrder();
    } catch (HttpException e) { // 发送HTTP请求失败
      // 调用e.getHttpRequest()获取请求打印日志或上报监控，更多方法见HttpException定义
      System.out.println("请求异常");
      e.printStackTrace();
    } catch (ServiceException e) { // 服务返回状态小于200或大于等于300，例如500
      // 调用e.getResponseBody()获取返回体打印日志或上报监控，更多方法见ServiceException定义
      System.out.println("服务异常");
      e.printStackTrace();
    } catch (MalformedMessageException e) { // 服务返回成功，返回体类型不合法，或者解析返回体失败
      // 调用e.getMessage()获取信息打印日志或上报监控，更多方法见MalformedMessageException定义
      System.out.println("解析异常");
      e.printStackTrace();
    }
  }

  /** 关闭订单 */
  public static void closeOrder() {

    CloseOrderRequest request = new CloseOrderRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    // 调用接口
    service.closeOrder(request);
  }

  /** JSAPI支付下单 */
  public static PrepayResponse prepay() {
    PrepayRequest request = new PrepayRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    request.setSpAppid("wx2ce3ab28ef605a98");
    request.setSpMchid("1720005941");
    request.setSubMchid("1720869343");
    request.setDescription("这是商品描述测试");
    request.setOutTradeNo("W1750838700399001947");
    // 设置失效时间为当前时间后15分钟的时间，帮我动态生成这个时间，形式为"2023-07-05T16:01:35+08:00"
    DateTime futureTime = DateUtil.offsetMinute(new Date(), 15);
    request.setTimeExpire(DateUtil.format(futureTime, "yyyy-MM-dd'T'HH:mm:ssXXX")); //这是选填字段
    request.setNotifyUrl("https://main-noticeably-chimp.ngrok-free.app/order/wechat/pay/callback");
    SettleInfo settleInfo = new SettleInfo();
    settleInfo.setProfitSharing(true);
    request.setSettleInfo(settleInfo);
    Amount amount = new Amount();
    amount.setTotal(1);
    request.setAmount(amount); //订单总金额，单位为分，整型。
    Payer payer = new Payer();
    payer.setSpOpenid("oboqt7XINauYESd10hv9Liaga4uw");
    request.setPayer(payer);

    // 调用接口
    return service.prepay(request);
  }

  /** 微信支付订单号查询订单 */
  public static Transaction queryOrderById() {

    QueryOrderByIdRequest request = new QueryOrderByIdRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    // 调用接口
    return service.queryOrderById(request);
  }

  /** 商户订单号查询订单 */
  public static Transaction queryOrderByOutTradeNo() {

    QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    // 调用接口
    return service.queryOrderByOutTradeNo(request);
  }
}